<?php

namespace Admin\Controller;

class SolarPanelController extends AdminbaseController {

    protected $solarPanelModel;
    protected $solarPanelLogModel;
    
    function _initialize() {
        parent::_initialize();
        $this->solarPanelModel = D("solarPanel");
        $this->solarPanelLogModel = D("solarPanelLog");
    }

    function index() {

        $count = $this->solarPanelModel->count();
        $page = $this->page($count, 20);
        $list = $this->solarPanelModel->alias('sp')
                ->join('left join c_area a on a.area_id = sp.area_id')
                ->field('sp.*,a.name as area_name')
                ->limit($page->firstRow . ',' . $page->listRows)->select();

        $this->assign("list", $list);
        $this->assign("page", $page->show('Admin'));
        $this->display();
    }
    
    function viewLog() {
        
        $id = I('id');

        $map['panel_id'] = $id;

        $day = I('day') ? I('day') : date('Y-m-d');

        $map['day'] = $day;
        
        $list = $this->solarPanelLogModel->where($map)->select();

        $voltage = array();
        $current = array();
        $temperature = array();
        $humidity = array();

        $i = 0;
        
        foreach($list as &$log) {

            $voltage[$i] = $log['voltage'];
            $current[$i] = $log['current'];
            $temperature[$i] = $log['temperature'];
            $humidity[$i] = $log['humidity'];

            $i++;
        }

        $this->assign('id', $id);
        $this->assign('day', $day);
        $this->assign('voltage', $voltage);
        $this->assign('current', $current);
        $this->assign('humidity', $humidity);
        $this->assign('temperature', $temperature);
        $this->assign('panelId', $id);
        $this->display();
        
    }
    
    function add() {
        $this->display();
    }

    /**
     *  添加规则
     */
    function add_post() {
        if (IS_POST) {
           
            $data['number'] = I('post.number');
            $data['area_id'] = I('post.areaId');
            $data['create_time'] = getCurrentDate();

            $this->solarPanelModel->add($data);

            $this->success("添加成功！", U('solarPanel/index'));
        }
    }

    /**
     *  编辑规则基本信息
     */
    function edit() {
        $id = I('get.id');
        $solarPanel = $this->solarPanelModel->where(array('panel_id' => $id))->find();
        $this->assign('solarPanel', $solarPanel);
        $this->display();
    }

    /**
     *  编辑规则基本信息
     */
    function edit_post() {
        if (IS_POST) {
            $id = I('post.id');

            $data = $this->solarPanelModel->where(array('panel_id' => $id))->find();
            
            $data['number'] = I('post.number');
            $data['area_id'] = I('post.areaId');

            $this->solarPanelModel->where(array('panel_id' => $id))->save($data);

            $this->success("更新成功！", U('solarPanel/index'));
        }
    }

    /**
     *  删除
     */
    function delete() {
        $id = intval(I('get.id'));
        $this->solarPanelModel->delete($id);
        $this->success("删除成功！", U('solarPanel/index'));
    }

}
